#import requests
#url="http://192.168.2.127:61623/mqtt"
#r = requests.get(url,"html.parser")
#print(r.text)
import paho.mqtt.client as mqtt
import time

HOST = "espush.cn"
PORT = 1883

def client_loop():
    client_id = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
    client = mqtt.Client(client_id)    # ClientId不能重复，所以使用当前时间
    client.username_pw_set("100552", "986bf265ae9cc1ede09e8db5b99ea77b")  # 必须设置，否则会返回「Connected with result code 4」
    client.on_connect = on_connect
    client.on_message = on_message
    client.connect(HOST, PORT, 60)
    client.loop_forever()

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    client.subscribe("deng/led")

def on_message(client, userdata, msg):
    print(msg.topic+"："+msg.payload.decode("utf-8"))  #gbk
#def on_push(client,userdata,msg):
    # publish.single("test", "你好 MQTT", qos=1, hostname=HOST, port=PORT, client_id=client_id,auth={'username': "admin", 'password': "password"})
if __name__ == '__main__':
    client_loop()